$(function(argument) {

	// 算总数放法
	function getSum() {
		// 被选中选框，计算商品价值
		//获取数量
		var sum = 0;
		var checkedGoods = $(".inputtwo");

		for (let i = 0; i < checkedGoods.length; i++) {
			if (checkedGoods[i].checked == true) {
				var a = checkedGoods[i];
				var count = Number($(a).parents('.oneGoogs').find('.text').html());
				var priceOne = Number($(a).parents('.oneGoogs').find('.red').html());
				sum += count * priceOne;
			}
		}
		$(".sum").html(sum.toFixed(2));
	}

	// 获取UID
	var uid = window.localStorage.getItem("user_id");
	var gid;
	var sid;
	if (uid == null) {
		window.location.href = "login.html";
		window.sessionStorage.setItem("loginFromCar", 1)
	} else {
		// 获取购物车数据
		$.ajax({
			url: 'http://www.teaweilai.com/app/Admin/AppCart/cartList',
			type: 'POST',
			dataType: 'json',
			data: {
				uid: uid
			},
			success: function(data) {
				// console.log(data);
				if (data.sing.goods == null) {
					$(".shopList").html("<h2 class='nothing' style='text-align: center;'>您还没有选择商品</h2>");

				} else {
					var strCar = template("shopCar", {
						car: data
					})
					$(".shopList").html(strCar);
					var checkedGoods = $(".inputtwo");
					for (let i = 0; i < checkedGoods.length; i++) {
						var a = checkedGoods[i];
						if (a.checked == true) {
							$(a).parents(".oneShop").eq(0).find(".inputone").prop("checked", $(this).parents(".oneShop").eq(0).find(".inputtwo").length === $(this).parents(".oneShop").eq(0).find(".inputtwo:checked").length);
						};
					}
				}
				getSum();
			}
		})
	}



	// 全选反选
	// 商户全选框
	$(document).on("click", ".inputone", function(e) {
			// e.preventDefault();
			$(this).parents(".oneShop").eq(0).find(".inputtwo").prop("checked", $(this).prop("checked"));
			getSum();
			sid = $(this).parents(".oneShop").find(".youkuan").attr('data-seller_id');
			// console.log(sid);

			$.ajax({
				url: 'http://www.teaweilai.com/app/Admin/AppCart/cartList',
				type: 'POST',
				dataType: 'json',
				data: {
					uid: uid,
					seller_id: sid
				},
				success: function(data) {
					// console.log(data);
				}
			})
			// return false;
		})
		// 商户下面复选框
	$(document).on("click", ".inputtwo", function(e) {
		// e.preventDefault();
		// console.log(123);
		$(this).parents(".oneShop").eq(0).find(".inputone").prop("checked", $(this).parents(".oneShop").eq(0).find(".inputtwo").length === $(this).parents(".oneShop").eq(0).find(".inputtwo:checked").length);
		getSum();
		gid = $(this).parents("li").attr('data-goods_id');
		$.ajax({
			url: 'http://www.teaweilai.com/app/Admin/AppCart/cartList',
			type: 'POST',
			dataType: 'json',
			data: {
				uid: uid,
				goods_id: gid
			},
			success: function(data) {
				// console.log(data);
			}
		})
		// return false;
	})


	// 点击删除 删除商品
	$(document).on("click", ".del", function() {
		$(".areYouSure").css({
			display: 'block'
		});
		gid = $(this).parents("li").attr('data-goods_id');
		var num = Number($(this).parents("ul").attr('data-num'));
		console.log(num);
		$that = $(this);
		$(".sureS").click(function() {
			$.ajax({
				url: 'http://www.teaweilai.com/app/Admin/AppCart/delCart',
				type: 'POST',
				dataType: 'json',
				data: {
					uid: uid,
					goods_id: gid
				},
				success: function(data) {
					num--;
					$that.parents("ul").attr('data-num', num);
					if (num == 0) {
						$that.parents(".oneShop").eq(0).remove();
					} else {
						$that.parents("li").eq(0).remove();
					}
					getSum();
				}
			})
			$(".areYouSure").css({
				display: 'none'
			});

		})

	})
	$(".cancel").click(function() {
			$(".areYouSure").css({
				display: 'none'
			});
		})
		//点击加减和变化价格
	var k;
	$(document).on("click", ".btnan", function() {
			k = Number($(this).siblings('.text').eq(0).html());
			if (Number($(this).attr('datanumber')) == 1) {
				if (k <= 1) {
					k == 1;
				} else {
					k--;
				}
			} else {
				k++;
			}
			$(this).siblings('.text').eq(0).html(k);
			gid = $(this).parents("li").attr('data-goods_id');
			console.log(gid);
			var change = $(this).attr('datanumber');
			$.ajax({
				url: 'http://www.teaweilai.com/app/Admin/AppCart/changeNum',
				type: 'POST',
				dataType: 'json',
				data: {
					uid: uid,
					goods_id: gid,
					change: change
				},
				success: function(data) {
					console.log(data);
				}
			})
			getSum();
		})
		// 点击结算
	$(".settle").click(function() {
			var chks = $(".inputtwo:checked").length;
			console.log(chks)
			if (chks == 0) {
				alert("请选择要结算的商品")
				return false;
			} else {
				window.location.href = "order_detail.html";
				window.sessionStorage.setItem("fromCar", "1")
			}

		})
		// 点击编辑，出现删除
	var isEdit = true;

	$(document).on("click", ".edit", function() {
		if (isEdit) {
			$(this).parents(".oneShop").eq(0).find('.del').css({
				display: 'block',
			});
			$(this).parents(".oneShop").eq(0).find(".btnan").css({
				display: 'block',
			});
			$(this).parents(".oneShop").eq(0).find(".edit").html("完成");
			isEdit = false;
		} else {
			isEdit = true;
			$(this).parents(".oneShop").eq(0).find(".del").css({
				display: 'none',
			});
			$(this).parents(".oneShop").eq(0).find(".btnan").css({
				display: 'none',
			});
			$(this).parents(".oneShop").eq(0).find(".edit").html("编辑");
		}
	})


	// 点击商品
	$(document).on("click", ".goods", function(e) {
		e.preventDefault();
		var gid = $(this).eq(0).attr('data-goods_id');
		window.sessionStorage.setItem("gid", gid);
		window.location.href = "tea_detail.html";
	})

	// 点击品牌商城中的商铺
	$(document).on("click", ".shops", function(e) {
		e.preventDefault();
		var sid = $(this).eq(0).attr('data-seller_id');
		window.sessionStorage.setItem("sid", sid);
		window.location.href = "waterBusinessperson.html";
	})
})